var ds;
function displaySitePage() {
	Ext.form.Field.prototype.msgTarget = 'side';
	Ext.QuickTips.init();

	var sm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	ds = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : DISPATCH_URL
		}),
		reader : new Ext.data.JsonReader({
			root : 'sites',
			id : 'id',
			fields : [ {
				name : 'id'
			}, {
				name : 'name'
			}, {
				name : 'code'
			} ]
		})
	});
	var gridCm = new Ext.grid.ColumnModel([ new Ext.grid.RowNumberer(), {
		header : TXT_site_name,
		dataIndex : 'name',
		width : 200
	}, {
		header : TXT_site_code,
		dataIndex : 'code',
		width : 200
	} ]);

	var btnCreate = new Ext.Button({
		text : TXT_common_create,
		tooltip : TXT_common_create,
		iconCls : 'icoAdd'
	});

	var btnEdit = new Ext.Button({
		text : TXT_common_edit,
		tooltip : TXT_common_edit,
		iconCls : 'icoDetail'
	});

	var btnDelete = new Ext.Button({
		text : TXT_common_delete,
		tooltip : TXT_common_delete,
		iconCls : 'icoDel'
	});
	btnCreate.on('click', function() {
		createSiteFormWin('');
	});
	btnDelete
			.on(
					'click',
					function() {
						if (sm.getCount() == 0) {
							Ext.MessageBox
									.alert(TXT_common_hint, TXT_user_need);
							return;
						}
						var record = sm.getSelected();
						Ext.MessageBox
								.confirm(
										TXT_common_hint,
										TXT_user_delete_confirmation,
										function(btn) {
											if (btn == 'yes') {
												BestvSearch.showWaitingWin();
												Ext.Ajax
														.request({
															url : DISPATCH_URL,
															method : 'POST',
															params : 'cmd=site&action=delete&id='
																	+ record.id,
															success : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																var s = response.responseText;
																if (s == '[error]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_access_fail);
																} else if (s == '[null]') {
																	Ext.MessageBox
																			.alert(
																					TXT_common_hint,
																					TXT_common_relogin,
																					function() {
																						window.location.href = LOGIN_URL;
																					});
																} else {
																	var result = Ext.util.JSON
																			.decode(s);
																	if (result.result == 'success') {
																		if (result.message == 'relateinfo') {
																			Ext.MessageBox
																					.alert(
																							TXT_common_hint,
																							TXT_site_delete_relateinfo);
																		} else {
																			Ext.MessageBox
																					.alert(
																							TXT_common_hint,
																							TXT_common_delete_success);
																			reloadSites();
																		}
																	}
																}
															},
															failure : function(
																	response,
																	options) {
																Ext.MessageBox
																		.hide();
																Ext.MessageBox
																		.alert(
																				TXT_common_hint,
																				TXT_common_access_fail);
															}
														});
											}
										});
					});
	btnEdit.on('click', function() {
		if (sm.getCount() == 0) {
			Ext.MessageBox.alert(TXT_common_hint, TXT_user_need);
			return;
		}
		var record = sm.getSelected();
		createSiteFormWin(record.id);
	});
	var grid = new Ext.grid.GridPanel({
		el : 'grid',
		ds : ds,
		cm : gridCm,
		sm : sm,
		stripeRows : true,
		loadMask : true,
		title : TXT_site_title,
		tbar : [ btnCreate, '-', btnEdit, '-', btnDelete ]
	});
	ds.baseParams = {
		cmd : 'site',
		action : 'find'
	};
	ds.on('load', function() {
		grid.getSelectionModel().selectFirstRow();
	});
	ds.load();

	BestvSearch.glb_mainPanel.add(grid);
	BestvSearch.drawMainFrame(TXT_app_name + ' - ' + TXT_site_title);
}

function reloadSites() {
	ds.reload();
}

Ext.onReady(displaySitePage);

function createSiteFormWin(siteId) {
	var sid = siteId;
	var siteForm = new Ext.FormPanel({

		labelAlign : 'right',
		labelWidth : 150,
		height : 130,
		width : 380,
		margins : '0 0 2 0',
		cmargins : '0 0 2 0',
		// bodyStyle:'margin-right:5px',
		frame : true,
		defaults : {
			width : 180
		},
		defaultType : 'textfield',
		items : [ {
			fieldLabel : TXT_site_code,
			id : 'siteCode',
			name : 'siteCode',
			allowBlank : false
		}, {
			fieldLabel : TXT_site_name,
			id : 'siteName',
			name : 'siteName',
			allowBlank : false
		} ]
	});

	var win = null;
	win = new Ext.Window(
			{
				title : TXT_site_title,
				height : 130,
				width : 380,
				autoScroll : false,
				layout : 'form',
				border : false,
				minimizable : false,
				maximizable : false,
				resizable : false,
				modal : true,
				layoutConfig : {
					animate : false
				},
				items : siteForm,
				buttonAlign : 'center',
				buttons : [
						{
							text : TXT_common_OK,
							handler : function() {
								if (!siteForm.form.isValid())
									return;
								var siteName = siteForm.findById('siteName')
										.getValue();
								var siteCode = siteForm.findById('siteCode')
										.getValue();
								if (siteCode == '') {
									Ext.MessageBox.alert(TXT_common_hint,
											TXT_site_code_not_null);
									return;
								}
								if (siteName == '') {
									Ext.MessageBox.alert(TXT_common_hint,
											TXT_site_name_not_null);
									return;
								}
								BestvSearch.showWaitingWin();
								Ext.Ajax
										.request({
											url : DISPATCH_URL,
											mehtod : 'post',
											params : 'cmd=site&action=save&id='
													+ sid + '&siteCode='
													+ siteCode + '&siteName='
													+ siteName,
											success : function(response,
													options) {
												Ext.MessageBox.hide();
												var s = response.responseText;
												if (s == 'error') {
													Ext.MessageBox
															.alert(
																	TXT_common_hint,
																	TXT_common_access_fail);
												} else if (s == 'null') {
													Ext.MessageBox
															.alert(
																	TXT_common_hint,
																	TXT_common_relogin,
																	function() {
																		window.location.href = '../eni/common/logout.jsp';
																	});
												} else {
													var result = Ext.util.JSON
															.decode(s);
													if (result.result == 'success') {
														if (result.message == 'new')
															Ext.MessageBox
																	.alert(
																			TXT_common_hint,
																			TXT_site_create_success,
																			function() {
																				win
																						.close();
																			});
														else if (result.message == 'edit')
															Ext.MessageBox
																	.alert(
																			TXT_common_hint,
																			TXT_edit_success,
																			function() {
																				win
																						.close();
																			});
														win.close();
														reloadSites();
													} else if (result.message == 'code') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_site_code_exist);
													} else if (result.message == 'name') {
														Ext.MessageBox
																.alert(
																		TXT_common_hint,
																		TXT_site_name_exist);
													}
												}
											},
											failure : function(respond, options) {
												Ext.MessageBox.hide();
												Ext.MessageBox.alert(
														TXT_common_hint,
														TXT_common_access_fail);
											}
										});
							}
						}, {
							text : TXT_common_cancel,
							handler : function() {
								win.close();
							}
						} ]
			});
	win.on('show', function() {
		if (sid != '') {
			Ext.Ajax.request({
				url : DISPATCH_URL,
				method : 'post',
				params : 'cmd=site&action=get&id=' + sid,
				success : function(response, options) {
					var s = response.responseText;
					if (s == '[error]')
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_access_fail);
					else if (s == '[null]') {
						Ext.MessageBox.alert(TXT_common_hint,
								TXT_common_relogin, function() {
									window.location.href = LOGIN_URL;
								});
					} else {
						var result = Ext.util.JSON.decode(s);
						siteForm.findById('siteCode').setValue(result.code);
						siteForm.findById('siteName').setValue(result.name);
					}
				},
				failure : function(response, options) {
					Ext.MessageBox.alert(TXT_common_hint,
							TXT_common_access_fail);
				}
			});
		}
	});
	win.show();
}
